<?php
namespace App\Controllers\Goods;
use \App\Libs\Base\Constant;
use App\Models\Goods\SearchHistory;

/**
*	搜索历史记录
*/
class SetHistory extends \App\Libs\Base\BaseAction
{
	 protected function beforeRun() {
         parent::beforeRun();
         $this->needLogin = TRUE;
     }
	public function execute() {
		$key = $this->request->get('key');
		$uid = $user = $this->session->user->id;
		//查询是否存在KEY
		$searchHistory = SearchHistory::find(array(
			'key = :key: and uid = :uid:',
			'bind' => array('key'=>$key,'uid'=>$uid),
		));
		if( !empty( $searchHistory ) ){
			//存在修改  原因在于以更新时间为排序目的
			$searchHistory->utime = date("Y-m-d H:i:s");
			$searchHistory->save();
			return \App\Libs\Views\Json::view(0, '成功', null);
		}else{
			//不存在 添加 
			$searchHistory = new SearchHistory();
			$searchHistory->uid = $uid;
			$searchHistory->key = $key;
			$searchHistory->ctime = date("Y-m-d H:i:s");
			$searchHistory->save();
			return \App\Libs\Views\Json::view(0, '成功', null);
		}
		
	}
}
